package com.abigdreamer.swing.app;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.border.EmptyBorder;

import com.abigdreamer.swing.component.JCButton;
import com.abigdreamer.swing.component.UIUtil;

import craky.component.JImagePane;
import craky.componentc.JCDialog;
import craky.layout.LineLayout;

public class FormWindow
  extends JCDialog
  implements ActionListener
{
  private static final long serialVersionUID = -7419167573324153596L;
  private JComponent content;
  private JCButton btnOk;
  private JCButton btnCancel;
  
  public FormWindow(Frame owner)
  {
    super(owner, false);
  }
  
  public FormWindow(Window window, String title, Dialog.ModalityType modalityType)
  {
    super(window, title, modalityType);
  }
  
  protected void initUI()
  {
    this.btnOk = new JCButton("确定");
    this.btnCancel = new JCButton("取消");
    
    JImagePane buttonPane = new JImagePane();
    buttonPane.setLayout(new LineLayout(5, 7, 7, 7, 0, 10, 10, 0));
    buttonPane.setBackground(new Color(255, 255, 255, 150));
    buttonPane.setPreferredSize(new Dimension(-1, 32));
    
    buttonPane.add(this.btnOk, "End");
    buttonPane.add(this.btnCancel, "End");
    
    this.btnOk.setPreferredSize(new Dimension(69, 21));
    this.btnOk.addActionListener(this);
    this.btnCancel.setPreferredSize(this.btnOk.getPreferredSize());
    this.btnCancel.addActionListener(this);
    
    this.content = ((JComponent)getContentPane());
    
    this.content.setBorder(new EmptyBorder(0, 2, 2, 2));
    this.content.setLayout(new BorderLayout());
    
    List<Component> formItems = createFormItems();
    
    Form form = new Form();
    form.setFormItems(formItems);
    
    this.content.setPreferredSize(new Dimension(310, 30 * formItems.size() + 15));
    
    this.content.add(buttonPane, "South");
    this.content.add(form, "Center");
    
    UIUtil.escAndEnterAction(this, this.btnOk, new AbstractAction()
    {
      private static final long serialVersionUID = 7092223290373790975L;
      
      public void actionPerformed(ActionEvent e)
      {
        FormWindow.this.cancel();
      }
    });
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        FormWindow.this.cancel();
      }
    });
    setDefaultCloseOperation(0);
    pack();
    setSize(new Dimension(310, 30 * formItems.size() + 80));
    setResizable(false);
    setLocationRelativeTo(getOwner());
  }
  
  public void showWindow()
  {
    initUI();
    setVisible(true);
  }
  
  protected void submit() {}
  
  protected void cancel()
  {
    dispose();
  }
  
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source == this.btnOk) {
      submit();
    } else if (source == this.btnCancel) {
      cancel();
    }
  }
  
  protected List<Component> createFormItems()
  {
    return null;
  }
}
